package com.timothy.android;

import java.util.List;

import com.timothy.answer.SingleSelectAnswer;
import com.timothy.question.Choice;
import com.timothy.question.QuestionBuilder;
import com.timothy.question.STCommonButtonAlpha;

import android.R.integer;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class LayoutX extends BaseQuestionActivity{
	private static final String LOG_TAG = "LayoutX";
	private LinearLayout layoutC;
	private TextView promotMsg = null;
	private STCommonButtonAlpha sTCommonButtonAlpha;
	
//	int msgAreaHeight;
	private LayoutSettingButton settingBtn = null;
	
	//initial setting
	public void initialSetting(int btnCount){
//		msgAreaHeight = screenHeight/4;
		
		int btnAreaHeight;
		
		int btnHeight = 0;
		int btnWidth = 0;
		Drawable btnDrawable = null;
		
		//setting btn height
		btnAreaHeight = (screenHeight*3)/4;
		if(btnCount==4){
			btnHeight = btnAreaHeight/2;
		}else if(btnCount==5||btnCount==6){
			btnHeight = btnAreaHeight/3;
		}else if(btnCount==7||btnCount==8){
			btnHeight = btnAreaHeight/4;
		}else if(btnCount==9||btnCount==10){
			btnHeight = btnAreaHeight/5;
		}
		//setting btn width
		btnWidth = screenWidth/2;
		//setting btn drawable
		btnDrawable = this.getResources().getDrawable(R.drawable.button);
		
		settingBtn = new LayoutSettingButton(btnHeight,btnWidth,btnDrawable);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layoutx);
        Log.i(LOG_TAG,"LayoutC:onCreate");
        
        layoutC = (LinearLayout) this.findViewById(R.id.layoutC);
		layoutC.setBackgroundColor(setting.layout_bg_color);
		layoutC.setPadding(setting.layout_padding,setting.layout_padding,setting.layout_padding,setting.layout_padding);
		
		//promotMsg setting
		promotMsg = (TextView) this.findViewById(R.id.promotMsg);
		promotMsg.setHeight(screenHeight/4);
		promotMsg.setBackgroundColor(setting.tv_msg_bg_color);
		promotMsg.setTextColor(setting.tv_msg_textcolor);
		promotMsg.setTextSize(setting.tv_msg_textcolor);
		
		Bundle bundle = this.getIntent().getExtras();
		String message = bundle.getString("message");
		Log.i(LOG_TAG,"message:"+message);
		
		sTCommonButtonAlpha = QuestionBuilder.parseSTButtonQuestion(message);		
		this.appendTitle("(SlideIndex:"+sTCommonButtonAlpha.getSlideIndex()+")");
		
		promotMsg.setText(sTCommonButtonAlpha.getQuestionName());
		List<Choice> choices = sTCommonButtonAlpha.getChoices();
		
		//initialSetting
		initialSetting(choices.size());
		
        GridView gridview = (GridView) findViewById(R.id.buttonsView);
        gridview.setNumColumns(2);
        gridview.setAdapter(new ButtonAdapter(this,choices,settingBtn));
        gridview.setOnItemClickListener(new OnItemClickListener() {
        	@Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        		Log.i(LOG_TAG,"onItemClick:position="+position);
        		Log.i(LOG_TAG,"onItemClick:id="+id);
        		
                Toast.makeText(LayoutX.this, "" + position, Toast.LENGTH_SHORT).show();
                SingleSelectAnswer answer = new SingleSelectAnswer();
				answer.setQuestion(sTCommonButtonAlpha);
				
				String answerMark = sTCommonButtonAlpha.getChoices().get(position).getMark();
				Log.i(LOG_TAG,"answerMark="+answerMark);
				answer.setMark(answerMark);
				sendAnswer(answer);
            }
        });
    }

}
